/*
 * This file is part of nettool4j.
 * 
 * rockframework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * rockframework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */
package net.woodstock.nettool4j.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;

import net.woodstock.nettool4j.AbstractRunnable;

public class OutputSocket extends AbstractRunnable {

	private ServerSocket	serverSocket;

	private int				listen;

	public OutputSocket(final int listen) {
		super();
		this.listen = listen;
	}

	@Override
	public void run() {
		try {
			this.getLogger().log(Level.INFO, "Creating socket");
			this.serverSocket = new ServerSocket(this.listen);

			while (this.isRunEnabled()) {
				this.getLogger().log(Level.INFO, "Running " + this.serverSocket.getLocalSocketAddress());
				Socket socket = this.serverSocket.accept();
				this.getLogger().log(Level.INFO, "Connection " + socket.getLocalAddress() + ":" + socket.getLocalPort());

				new Thread(new OutputSocketHandler(socket)).start();
			}
		} catch (IOException e) {
			this.getLogger().log(Level.WARNING, e.getMessage(), e);
		}
	}

}
